package com.iMusic.businessLogicLayer;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.iMusic.businessLogicLayer.orderStatus.OrderStatus;
import com.iMusic.integrationLayer.DBConnection;
import com.iMusic.integrationLayer.DBConnectionManager;

public class OrderContent {

	long orderContentId = 0;
	Integer productId = null;
	Integer quantity = null;
	Float price = null;
	
	public OrderContent (Integer pId, Integer qtt, Float pri) {
		productId = pId;
		quantity = qtt;
		price = pri;
	}
	
	
	/*
	 * Searches for order content
	 */
	static public List<OrderContent> searchOrderContents(int orderId) throws SQLException {
		List<OrderContent> ocList = new ArrayList<OrderContent>();
		
		DBConnectionManager dbConnMgr = DBConnectionManager.getDBConnectionManager();
		DBConnection dbConn = null;
		dbConn = dbConnMgr.getDBConnection();
		dbConn.connect();
		
		String sqlStmt = "SELECT OrderContentId, OrderId, ProductId, Quantity, Price FROM OrderContent " +
				"WHERE OrderId = " + orderId;

		ResultSet rs = dbConn.query(sqlStmt);

		while (rs != null && rs.next()) {
		
			int pId = rs.getInt("ProductId");
			int qtt = rs.getInt("Quantity");
			float pri = rs.getFloat("Price");
			OrderContent oc = new OrderContent(pId, qtt, pri);
			oc.setOrderContentId(rs.getInt("OrderContentId"));			
			
			ocList.add(oc);
		}

		rs.close();
		rs = null;
		
		dbConn.disconnect();
		dbConnMgr.freeDBConnection(dbConn);
		
		return ocList;
	}
	
	public long getOrderContentId() {
		return orderContentId;
	}
	public void setOrderContentId(long orderContentId) {
		this.orderContentId = orderContentId;
	}
	public Integer getProductId() {
		return productId;
	}
	public void setProductId(Integer productId) {
		this.productId = productId;
	}
	public Integer getQuantity() {
		return quantity;
	}
	public void setQuantity(Integer quantity) {
		this.quantity = quantity;
	}
	public Float getPrice() {
		return price;
	}
	public void setPrice(Float price) {
		this.price = price;
	}
	
}
